package com.jgssp.ad.sdk;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.jgssp.ad.config.TTAdManagerHolder;
import com.jgssp.ad.constant.AdConstant;
import com.jgssp.ad.utils.UIUtils;

public class SplashActivity extends BaseActivity {

    private static final String TAG = "JgAds" + SplashActivity.class.getSimpleName();
    private boolean mIsHalfSize = false;
    private static final int AD_TIME_OUT = 3000;

    private TTAdNative mTTAdNative;

    private FrameLayout mSplashContainer;

    //是否强制跳转到主页面
    private boolean mForceGoMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mSplashContainer = findViewById(R.id.splash_container);

        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        initListener();
        initAdLoader();
    }

    @Override
    public void initListener() {
    }


    @Override
    public void initAdLoader() {
        loadSplashAd();
    }

    /**
     * 加载开屏广告
     */
    private void loadSplashAd() {
        //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        float splashWidthDp = UIUtils.getScreenWidthDp(this);
        int splashWidthPx = UIUtils.getScreenWidthInPx(this);
        int screenHeightPx = UIUtils.getScreenHeight(this);
        float screenHeightDp = UIUtils.px2dip(this, screenHeightPx);
        float splashHeightDp;
        int splashHeightPx;


        if (mIsHalfSize) {
            // 开屏高度 = 屏幕高度 - 下方预留的高度，demo中是预留了屏幕高度的1/5，因此开屏高度传入 屏幕高度*4/5
            splashHeightDp = screenHeightDp * 4/5.f;
            splashHeightPx = (int) (screenHeightPx * 4/5.f);
        } else {
            splashHeightDp = screenHeightDp;
            splashHeightPx = screenHeightPx;
        }


        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(AdConstant.SPLASH)
                //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
                //view宽高等于图片的宽高
                .setExpressViewAcceptedSize(splashWidthDp, splashHeightDp) // 单位是dp
                .setImageAcceptedSize(splashWidthPx, splashHeightPx) // 单位是px
                .build();

        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.CSJSplashAdListener() {

            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {

            }

            @Override
            public void onSplashLoadFail(CSJAdError error) {
                goToMainActivity();
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd ad) {
                if (ad == null) {
                    return;
                }
                ad.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
                    @Override
                    public void onSplashAdShow(CSJSplashAd csjSplashAd) {

                    }

                    @Override
                    public void onSplashAdClick(CSJSplashAd csjSplashAd) {

                    }

                    @Override
                    public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                        goToMainActivity();
                    }
                });
                mSplashContainer.addView(ad.getSplashView());
            }

            @Override
            public void onSplashRenderFail(CSJSplashAd ad, CSJAdError csjAdError) {
                goToMainActivity();
            }
        }, AD_TIME_OUT);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        //判断是否该跳转到主页面
        if (mForceGoMain) {
            goToMainActivity();
        }
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mForceGoMain = true;
    }

    /**
     * 跳转到主页面
     */
    private void goToMainActivity() {
        Log.d("JgAds", "goToMainActivity");
        finish();
    }

}